home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / v8n06.arc / TRYBSRCH.ASM < prev    next >
Encoding:
Assembly Source File  |  1989-02-28  |  6.8 KB  |  194 lines

  1. ; TRYBSRCH.ASM  Demo of BSEARCH routine (MS-DOS version),
  2. ;               searches file previously created by MAKENIF.EXE.        
  3. ; Copyright (c) 1989 Ziff Communications Co.
  4. ; PC Magazine * Ray Duncan
  5. ;
  6. ; The user is prompted to enter a search key.  The first
  7. ; 8 characters of the key are used to search TESTFILE.DAT,
  8. ; using the binary search algorithm.  The success or failure 
  9. ; of the search is reported along with the record number (if found).
  10. ;
  11. ; Each record in TESTFILE.DAT is RSIZE bytes long.  Bytes 
  12. ; 0 to (KSIZE-1) of the record are the ASCII key for searches
  13. ; by BSEARCH.  Bytes KSIZE to (RSIZE-1) of the record
  14. ; are initialized to zero and are not used.  RSIZE, KSIZE,
  15. ; and the key offset within the record must be kept synchronized
  16. ; with MAKENIF.C.
  17. ;
  18. ; To build TRYBSRCH.EXE you also need BSEARCH.ASM, ITOA.ASM,
  19. ; and STRINGS1.ASM.  Enter the following series of commands:
  20. ;
  21. ;       MASM TRYBSRCH;
  22. ;       MASM BSEARCH;
  23. ;       MASM ITOA;
  24. ;       MASM STRINGS1;
  25. ;       LINK TRYBSRCH+BSEARCH+ITOA+STRINGS1;
  26.  
  27. stdin   equ     0               ; standard input handle
  28. stdout  equ     1               ; standard output handle
  29.  
  30. cr      equ     0dh             ; ASCII carriage return
  31. lf      equ     0ah             ; ASCII line feed
  32. blank   equ     20h             ; ASCII blank
  33.  
  34. rsize   equ     64              ; TESTFILE.DAT record size
  35. ksize   equ     8               ; TESTFILE.DAT key size
  36. koffs   equ     0               ; offset of key within record
  37.  
  38. _TEXT   segment word public 'CODE'
  39.  
  40.         assume  cs:_TEXT,ds:_DATA
  41.  
  42.         extrn   itoa:near
  43.         extrn   bsearch:near
  44.  
  45. main    proc    near
  46.  
  47.         mov     ax,_DATA        ; make our data segment
  48.         mov     ds,ax           ; addressable...
  49.         mov     es,ax
  50.  
  51.         cld                     ; string ops safety first
  52.  
  53.                                 ; open the file TESTFILE.DAT
  54.         mov     dx,offset fname ; DS:DX = filename
  55.         mov     ax,3d00h        ; fxh 3d00h = open, read-only
  56.         int     21h             ; transfer to MS-DOS
  57.         jnc     main1           ; jump if open successful
  58.  
  59.                                 ; open failed, display error
  60.                                 ; message and exit...
  61.         mov     dx,offset msg1  ; DS:DX = message address
  62.         mov     cx,msg1_len     ; CX = message length
  63.         mov     bx,stdout       ; BX = standard output handle
  64.         mov     ah,40h          ; fxn 40h = write
  65.         int     21h             ; transfer to MS-DOS
  66.         jmp     main4           ; go perform final exit
  67.  
  68. main1:  mov     fhandle,ax      ; save file handle
  69.  
  70.                                 ; find filesize in bytes...
  71.         mov     bx,ax           ; BX = handle
  72.         mov     cx,0            ; CX:DX = file offset of 0
  73.         mov     dx,0
  74.         mov     ax,4202h        ; fxn 42h, subf. 2 = seek
  75.                                 ; relative to end of file
  76.         int     21h             ; transfer to MS-DOS
  77.  
  78.         mov     bx,rsize        ; filesize / bytes per record
  79.         div     bx              ; = records in file
  80.         mov     frecs,ax
  81.         
  82.                                 ; display "Enter search key: "
  83. main2:  mov     dx,offset msg2  ; DS:DX = message address
  84.         mov     cx,msg2_len     ; CX = message length
  85.         mov     bx,stdout       ; BX = handle
  86.         mov     ah,40h          ; Fxn 40H = write
  87.         int     21h             ; transfer to MS-DOS
  88.  
  89.         mov     cx,ksize        ; zero out previous key
  90.         mov     di,offset kval
  91.         xor     al,al
  92.         rep stosb
  93.  
  94.         mov     cx,6            ; remove previous record
  95.         mov     di,offset msg3a ; number from output string
  96.         mov     al,blank
  97.         rep stosb
  98.  
  99.                                 ; get search key from user
  100.         mov     dx,offset kval  ; DS:DX = input buffer
  101.         mov     cx,80           ; CX = max input length   
  102.         mov     bx,stdin        ; BX = handle             
  103.         mov     ah,3fh          ; Fxn 3FH = read          
  104.         int     21h             ; transfer to MS-DOS      
  105.  
  106.         cmp     ax,2            ; was anything entered?
  107.         je      main4           ; empty line, exit
  108.  
  109.         mov     bx,ax           ; remove CR-LF from input
  110.         mov     word ptr [bx+kval-2],0
  111.  
  112.                                 ; set up for binary search
  113.         mov     bx,fhandle      ; file handle
  114.         mov     cx,rsize        ; record size   
  115.         mov     dx,offset fbuff ; record buffer
  116.         mov     si,0            ; first (left) record
  117.         mov     di,frecs        ; last (right) record
  118.         dec     di
  119.         mov     bp,offset key   ; key structure address
  120.         call    bsearch         ; call search routine
  121.         jnz     main3           ; jump if record not found
  122.  
  123.         mov     si,offset msg3a ; convert record number
  124.         mov     cx,10           ; to ASCII and store in output
  125.         call    itoa
  126.  
  127.                                 ; display 'record number is nnnn'
  128.         mov     dx,offset msg3  ; DS:DX = message address
  129.         mov     cx,msg3_len     ; CX = message length
  130.         mov     bx,stdout       ; BX = standard output handle
  131.         mov     ah,40h          ; Fxn 40h = write
  132.         int     21h             ; transfer to MS-DOS
  133.  
  134.         jmp     main2           ; get another search key
  135.  
  136. main3:                          ; display 'record not found'
  137.         mov     dx,offset msg4  ; DS:DX = message address
  138.         mov     cx,msg4_len     ; CX = message length
  139.         mov     bx,stdout       ; BX = standard output handle
  140.         mov     ah,40h          ; Fxn 40h = write
  141.         int     21h             ; transfer to MS-DOS
  142.  
  143.         jmp     main2           ; get another search key
  144.  
  145. main4:  mov     ax,4c00h        ; final exit to MS-DOS
  146.         int     21h
  147.  
  148. main    endp
  149.  
  150. _TEXT   ends
  151.  
  152.  
  153. _DATA   segment word public 'DATA'
  154.  
  155. fname   db      'TESTFILE.DAT',0 ; file created by MAKENIF.C
  156. fhandle dw      ?               ; handle for TESTFILE.DAT
  157. frecs   dw      ?               ; records in file
  158. fbuff   db      rsize dup (0)   ; file record buffer
  159.  
  160. key     dw      ksize           ; length of key data
  161.         dw      koffs           ; offset of key within record
  162. kval    db      80 dup (0)      ; actual key data
  163.  
  164. msg1    db      cr,lf
  165.         db      'Can''t open TESTFILE.DAT'
  166.         db      cr,lf
  167. msg1_len equ $-msg1
  168.  
  169. msg2    db      cr,lf
  170.         db      'Enter search key: '
  171. msg2_len equ $-msg2
  172.  
  173. msg3    db      cr,lf
  174.         db      'Record number is: '
  175. msg3a   db      6 dup (blank)
  176.         db      cr,lf
  177. msg3_len equ $-msg3
  178.  
  179. msg4    db      cr,lf
  180.         db      'Record not found'
  181.         db      cr,lf
  182. msg4_len equ $-msg4
  183.  
  184. _DATA   ends
  185.  
  186.  
  187. STACK   segment para stack 'STACK'
  188.         
  189.         db      4096 dup (?)
  190.  
  191. STACK   ends
  192.  
  193.         end     main
  194.